package com.vrv.app.cyberspace.modules.sys.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.vrv.app.cyberspace.common.utils.HttpContextUtils;
import com.vrv.app.cyberspace.modules.sys.entity.*;
import com.vrv.app.cyberspace.modules.sys.mapper.*;
import com.vrv.app.cyberspace.modules.sys.service.ICirculationRecordTableService;
import com.vrv.app.cyberspace.modules.sys.service.IEventApprovalTableService;
import com.vrv.app.cyberspace.modules.sys.service.IRiskApprovalTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 流转记录表 服务实现类
 * </p>
 *
 * @author sm
 * @since 2018-01-15
 */
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Service
@Transactional
public class CirculationRecordTableServiceImpl extends ServiceImpl<CirculationRecordTableMapper, CirculationRecordTable> implements ICirculationRecordTableService {

    @Autowired
    private CirculationRecordTableMapper circulationRecordTableMapper;
    @Autowired
    private EventFeedbackTableMapper eventFeedbackTableMapper;
    @Autowired
    private RiskFeedbackTableMapper riskFeedbackTableMapper;
    @Autowired
    private IEventApprovalTableService eventApprovalTableService;
    @Autowired
    private IRiskApprovalTableService riskApprovalTableService;
    @Autowired
    private RiskManageTableMapper riskManageTableMapper;
    @Autowired
    private EventManageTableMapper eventManageTableMapper;

    @Override
    public int addCirculationRecord(CirculationRecordTable circulationRecordTable) {
        int result;
        try {
            UserTable userTable = (UserTable) HttpContextUtils.getHttpServletRequest().getSession().getAttribute("user");
            Integer flag = circulationRecordTable.getExStatus();//1--确认;2--继续跟踪;3--终止反馈
            String fId = circulationRecordTable.getfId();
            //1.在日志表中生成一条日志记录
            insertRecord(userTable.getLinkdoodName(), circulationRecordTable.getContent(), new Date(), fId);
            //2.根据fId查询反馈表中的记录,将记录内容复制到日志记录表中
            if (3 == circulationRecordTable.getInfoType()) {//事件
                EventFeedbackTable eventFeedbackTable = eventFeedbackTableMapper.selectById(fId);
                //3.操作反馈表中的记录
                eventFeedbackTable.setExStatus(circulationRecordTable.getExStatus());
                eventFeedbackTable.setStatus(2 == flag ? 0 : 2);//  确定/终止反馈--(办结)2;继续跟踪--(办结)0
                if (2 == flag) {//继续跟踪
                    eventFDSetNull(eventFeedbackTable);
                }
                eventFeedbackTableMapper.updateAllColumnById(eventFeedbackTable);
                // 进入处理流程
                updateEBaseCondition(eventFeedbackTable);
            } else if (2 == circulationRecordTable.getInfoType()) {//预警
                RiskFeedbackTable riskFeedbackTable = riskFeedbackTableMapper.selectById(fId);
                //3.操作反馈表中的记录
                riskFeedbackTable.setExStatus(circulationRecordTable.getExStatus());
                riskFeedbackTable.setStatus(2 == flag ? 0 : 2);//   确定/终止反馈--(办结)2;继续跟踪--(办结)0
                if (2 == flag) {//继续跟踪
                    riskFDSetNull(riskFeedbackTable);
                }
                riskFeedbackTableMapper.updateAllColumnById(riskFeedbackTable);
                // 进入处理流程
                updateRBaseCondition(riskFeedbackTable);
            }
            result = 1;
        } catch (Exception e) {
            result = 0;
            e.printStackTrace();
        }
        return result;
    }

    private void riskFDSetNull(RiskFeedbackTable riskFeedbackTable) {
        riskFeedbackTable.setReadUserID(null);
        riskFeedbackTable.setReadTime(null);
        riskFeedbackTable.setResponse(null);
        riskFeedbackTable.setUnits(null);
        riskFeedbackTable.setDevices(null);
        riskFeedbackTable.setAffectedUnits(null);
        riskFeedbackTable.setInfectedDevices(null);
        riskFeedbackTable.setKeyFacilityNumber(null);
        riskFeedbackTable.setDumpDevices(null);
        riskFeedbackTable.setCleanKeyFacility(null);
    }

    private void eventFDSetNull(EventFeedbackTable eventFeedbackTable) {
        eventFeedbackTable.setReadUserID(null);
        eventFeedbackTable.setReadTime(null);
        eventFeedbackTable.setSituation(null);
        eventFeedbackTable.setSystemName(null);
        eventFeedbackTable.setIp(null);
        eventFeedbackTable.setDomain(null);
        eventFeedbackTable.setBehindUrl(null);
        eventFeedbackTable.setOsVersion(null);
        eventFeedbackTable.setAppName(null);
        eventFeedbackTable.setSystemPurpose(null);
        eventFeedbackTable.setUnit(null);
        eventFeedbackTable.setAttackRegion(null);
        eventFeedbackTable.setAttackSource(null);
    }

    private void updateRBaseCondition(RiskFeedbackTable riskFeedbackTable) {
        RiskManageTable rmt = riskManageTableMapper.selectById(riskFeedbackTable.getBaseID());
        List<String> list = JSON.parseArray(rmt.getDownsSet(), String.class);
        list.addAll(JSON.parseArray(rmt.getManufacturers(), String.class));
        int having = riskFeedbackTableMapper.selectCount(new EntityWrapper<RiskFeedbackTable>()
                .in("exStatus", new Integer[]{1, 3})
                .eq("baseID", rmt.getBaseID())
                .in("orgID", list));
        if (having == list.size()) {
            riskApprovalTableService.feedbackRiskApproval(rmt.getBaseID(), "反馈完毕");
        }
    }

    private void updateEBaseCondition(EventFeedbackTable eventFeedbackTable) {
        EventManageTable emt = eventManageTableMapper.selectById(eventFeedbackTable.getBaseID());
        List<String> list = JSON.parseArray(emt.getDownsSet(), String.class);
        list.addAll(JSON.parseArray(emt.getManufacturers(), String.class));
        int having = eventFeedbackTableMapper.selectCount(new EntityWrapper<EventFeedbackTable>()
                .in("exStatus", new Integer[]{1, 3})
                .eq("baseID", emt.getBaseID())
                .in("orgID", list));
        if (having == list.size()) {
            eventApprovalTableService.feedbackEventApproval(emt.getBaseID(), "反馈完毕");
        }
    }

    @Override
    public List<CirculationRecordTable> listCirculationRecord(CirculationRecordTable circulationRecordTable) {
        return circulationRecordTableMapper.selectList(new EntityWrapper<CirculationRecordTable>()
                .eq("fId", circulationRecordTable.getfId())
                .orderBy(true, "time", false));
    }

    @Override
    public int batchCirculationRecord(CirculationRecordTable circulationRecordTable) {
        List<String> orgIDs = JSON.parseArray(circulationRecordTable.getOrgIDs(), String.class);
        int result = 0;
        switch (circulationRecordTable.getInfoType()) {
            case 2://预警
                Wrapper<RiskFeedbackTable> rew = new EntityWrapper<RiskFeedbackTable>().eq("baseID", circulationRecordTable.getBaseID()).in("orgID", orgIDs);
                List<RiskFeedbackTable> rlist = riskFeedbackTableMapper.selectList(rew);
                for (RiskFeedbackTable r : rlist) {
                    circulationRecordTable.setfId(r.getId());
                    result += addCirculationRecord(circulationRecordTable);
                }
                break;
            case 3://事件
                Wrapper<EventFeedbackTable> eew = new EntityWrapper<EventFeedbackTable>().eq("baseID", circulationRecordTable.getBaseID()).in("orgID", orgIDs);
                List<EventFeedbackTable> elist = eventFeedbackTableMapper.selectList(eew);
                for (EventFeedbackTable e : elist) {
                    circulationRecordTable.setfId(e.getId());
                    result += addCirculationRecord(circulationRecordTable);
                }
                break;
        }
        return result;
    }

    /**
     * 保存流转记录
     */
    private void insertRecord(String userName, String content, Date date, String fId) {
        CirculationRecordTable cTem = new CirculationRecordTable();
        cTem.setOperator(userName);
        cTem.setTime(date);
        cTem.setContent(content);
        cTem.setfId(fId);
        circulationRecordTableMapper.insert(cTem);
    }
}
